package com.example.shop.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.shop.modules.sys.service.IGoodsTypeService;
import com.example.shop.persistence.entity.GoodsType;
import com.example.shop.persistence.mapper.GoodsMapper;
import com.example.shop.persistence.mapper.GoodsTypeMapper;
import com.example.shop.utils.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.UpdateProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.example.shop.utils.JSONResult.renderSuccess;

@Api(tags = "商品分类接口")
@RestController
@RequestMapping("/sys/goodsType")
public class GoodsTypeController {
    @Autowired
    IGoodsTypeService goodsTypeService;

    @ApiOperation(value = "查询分类列表",notes = "查询商品全部分类，不分页，不分级")
    @GetMapping("/list")
    public JSONResult getGoodsTypeList(){
        List<GoodsType> list = goodsTypeService.list();
        return renderSuccess(list);
    }

    @ApiOperation(value = "查询下级分类",notes = "根据上级分类ID，查询它的下级分类; 如果ID为空则查询一级分类")
    @GetMapping("/subordinate")
    public JSONResult getSubTypeList(Integer id){
        QueryWrapper qw = new QueryWrapper();
        // 查询父级ID 是目标ID的
        if(id == null){
            qw.isNull("parentID");
        }else{
            qw.eq("parentID", id);
        }
        return renderSuccess(goodsTypeService.list(qw));
    }

}
